#include <butil/logging.h>
#include <brpc/server.h>
#include "main.pb.h"

class EchoServiceImpl:public example::EchoService 
{
public:
    EchoServiceImpl()
    {}

    ~EchoServiceImpl()
    {}

    void Echo(google::protobuf::RpcController* controller,
              const example::EchoRequest* request,
              example::EchoResponse* response,
              google::protobuf::Closure* done)
    {
        std::cout << "收到消息:" << request->message() << std::endl;

        response->set_message("server 回显:" + request->message() );
        done->Run();
    }
};

int main()
{   
    //1,关闭brpc的默认输出
    brpc::ServerOptions options;
    // options.log_level = brpc::LOG_LEVEL_ERROR;
    options.idle_timeout_sec = -1; //连接空闲超时时间-超时后连接被关闭
    options.num_threads = 1; // io线程数量

    //2,构造服务器对象
    brpc::Server server;
    EchoServiceImpl echo_service;
    //向服务器对象中，新增EchoService服务
    if(server.AddService(&echo_service, brpc::ServiceOwnership::SERVER_DOESNT_OWN_SERVICE) != 0)
    {
        std::cout << "AddService failed" << std::endl;
        return -1;
    }
    //3,启动服务器
    if(server.Start(8080, &options) != 0)
    {
        std::cout << "Start failed" << std::endl;
        return -1;
    }
    server.RunUntilAskedToQuit();//修改等待运行结束
    return 0;
}